package com.yanqu.road.logic.pb;

import com.yanqu.road.entity.player.*;
import com.yanqu.road.pb.patrons.CombLeaderProto;
import com.yanqu.road.pb.patrons.PatronsProto;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class PatronsPb {

	public static PatronsProto.PatronsSkillTempMsg.Builder parsePatronsSkillTempMsg(UserPatronsSkill patronsSkill) {
		if(null == patronsSkill){
			return null;
		}
		PatronsProto.PatronsSkillTempMsg.Builder skillMsg = PatronsProto.PatronsSkillTempMsg.newBuilder();
		skillMsg.setSkillId(patronsSkill.getSkillId());
		skillMsg.setSkillLv(patronsSkill.getSkillLv());
		skillMsg.setSkinSkillLevel(patronsSkill.getSkinSkillLevel());
		skillMsg.setType(patronsSkill.getType());
		String extraParams = patronsSkill.getExtraParams();
		if(null == extraParams){
			extraParams = "";
		}
		skillMsg.setExtraParams(extraParams);
		return skillMsg;
	}

	public static PatronsProto.PatronsTempMsg.Builder parsePatronsTempMsg(UserPatrons userPatrons) {
		if(null == userPatrons){
			return null;
		}
		PatronsProto.PatronsTempMsg.Builder msg = PatronsProto.PatronsTempMsg.newBuilder();
		msg.setPatronsId(userPatrons.getPatronsId());
		msg.setLevel(userPatrons.getLevel());
		msg.setStageLv(userPatrons.getStageLv());
		msg.setBreakLv(userPatrons.getBreakLv());
		msg.setAddAbility(userPatrons.getAddAbility().toString());
		msg.setTradeWarAddEarnSpeed(userPatrons.getTradeWarAddEarnSpeed().toString());
		msg.setSkinId(userPatrons.getSkinId());
		msg.setOreWarEarnSpeed(userPatrons.getOreWarEarnSpeed().toString());
		msg.setCookExp(userPatrons.getCookExp());
		msg.setBaiYeExp(userPatrons.getBaiYeExp());
		msg.setChouSuanExp(userPatrons.getChouSuanExp());
		msg.setPromotionId(userPatrons.getShowPromotionId());
		msg.setTeacherExp(userPatrons.getTeacherExp());
		msg.setKeepSakeTime(userPatrons.getKeepSakeTime());
		msg.setKeepSakePatronsId(userPatrons.getShowKeepSakePatronsId());
		msg.setItemAddQualification(userPatrons.getItemAddQualification());
		if (null != userPatrons.getOriginalSkillList()) {
            for (UserPatronsSkill skill : userPatrons.getOriginalSkillList()) {
				if (skill.getIsDelete() == 1) {
					continue;
				}
                PatronsProto.PatronsSkillTempMsg.Builder skillMsg = parsePatronsSkillTempMsg(skill);
                if(null != skillMsg) {
                    msg.addSkillList(skillMsg);
                }
            }
        }
		msg.setGrandChildEarnSpeed(userPatrons.getShowGrandSpeed().toString());
		msg.setIsLinkageSwitch(userPatrons.isLinkageSwitch());
		msg.setTotalAbility(userPatrons.getAbility().toString());

		for (UserPatronsContract patronsContract : userPatrons.getPatronsContractMap().values()) {
			PatronsProto.PatronsContractBeautyTemp.Builder contractBuilder = PatronsProto.PatronsContractBeautyTemp.newBuilder();
			contractBuilder.setPos(patronsContract.getPos());
			contractBuilder.setBeautyId(patronsContract.getBeautyId());
			msg.addContractBeauty(contractBuilder);
		}

		PatronsProto.PatronsAbilityCalcTempMsg.Builder calcBuilder = PatronsProto.PatronsAbilityCalcTempMsg.newBuilder();
		calcBuilder.setQualification((int)userPatrons.getTotalQualification());
		calcBuilder.setBaseQualificationAdd(userPatrons.getCalcAbilityParam().toString());
		calcBuilder.setAdditionRatio(userPatrons.getAbilityAdditionRatio().toString());
		msg.setAbilityCalc(calcBuilder);
		return msg;
	}

	public static PatronsProto.PatronsSimpleTempMsg.Builder parsePatronsSimpleTempMsg(UserPatrons userPatrons) {
		if(null == userPatrons){
			return null;
		}
		PatronsProto.PatronsSimpleTempMsg.Builder msg = PatronsProto.PatronsSimpleTempMsg.newBuilder();
		msg.setPatronsId(userPatrons.getPatronsId());
		msg.setLevel(userPatrons.getLevel());
		msg.setStageLv(userPatrons.getStageLv());
		msg.setSkinId(userPatrons.getSkinId());
		msg.setPromotionId(userPatrons.getShowPromotionId());
		msg.setAbility(userPatrons.getAbility().toString());
		msg.setQuality(userPatrons.getQualification());
		msg.setBlood(userPatrons.getTotalBlood().toString());
		return msg;
	}

	public static PatronsProto.PatronsListMsg.Builder parsePatronsListMsg(List<UserPatrons> patronsList) {
		PatronsProto.PatronsListMsg.Builder listMsg = PatronsProto.PatronsListMsg.newBuilder();
		if(null != patronsList) {
			for (UserPatrons patrons : patronsList) {
				PatronsProto.PatronsTempMsg.Builder msg = parsePatronsTempMsg(patrons);
				if (null != msg) {
					listMsg.addPatronsList(msg);
				}
			}
		}
		return listMsg;
	}

	public static PatronsProto.CrossPatronsTempMsg.Builder parseCrossPatronsTempMsg(UserPatrons userPatrons) {
		if(null == userPatrons){
			return null;
		}
		PatronsProto.CrossPatronsTempMsg.Builder msg = PatronsProto.CrossPatronsTempMsg.newBuilder();
		msg.setPatronsId(userPatrons.getPatronsId());
		msg.setAbility(userPatrons.getAbility().toString());
		if (null != userPatrons.getSkillList()) {
			for (UserPatronsSkill skill : userPatrons.getSkillList()) {
				PatronsProto.PatronsSkillTempMsg.Builder skillMsg = parsePatronsSkillTempMsg(skill);
				if(null != skillMsg) {
					msg.addSkillList(skillMsg);
				}
			}
		}
		return msg;
	}

	public static UserPatrons parseUserPatrons(long userId, PatronsProto.CrossPatronsTempMsg patronsTempMsg){
		UserPatrons userPatrons = new UserPatrons();
		userPatrons.setUserId(userId);
		userPatrons.setPatronsId(patronsTempMsg.getPatronsId());
		userPatrons.setAbility(new BigInteger(patronsTempMsg.getAbility()));
		List<UserPatronsSkill> skillList = new ArrayList<>();
		for(PatronsProto.PatronsSkillTempMsg skillTempMsg : patronsTempMsg.getSkillListList()){
			UserPatronsSkill userPatronsSkill = new UserPatronsSkill();
			userPatronsSkill.setUserId(userId);
			userPatronsSkill.setPatronsId(patronsTempMsg.getPatronsId());
			userPatronsSkill.setType(skillTempMsg.getType());
			userPatronsSkill.setSkillId(skillTempMsg.getSkillId());
			userPatronsSkill.setSkillLv(skillTempMsg.getSkillLv());
			userPatronsSkill.setSkinSkillLevel(skillTempMsg.getSkinSkillLevel());
			skillList.add(userPatronsSkill);
		}
		userPatrons.setSkillList(skillList);
		return userPatrons;
	}

	public static CombLeaderProto.CombLeaderUserDataSyncMsg.Builder parseCombLeaderUserDataSyncMsg(List<UserCombLeaderData> dataList) {
		CombLeaderProto.CombLeaderUserDataSyncMsg.Builder msg = CombLeaderProto.CombLeaderUserDataSyncMsg.newBuilder();
		if (null != dataList) {
			for (UserCombLeaderData data : dataList) {
				CombLeaderProto.CombLeaderTempMsg tempMsg = parseCombLeaderTempMsg(data);
				if (null != tempMsg) {
					msg.addCombLeader(tempMsg);
				}
			}
		}
		return msg;
	}

	public static CombLeaderProto.CombLeaderTempMsg parseCombLeaderTempMsg(UserCombLeaderData data) {
		if (null == data) {
			return null;
		}
		CombLeaderProto.CombLeaderTempMsg.Builder msg = CombLeaderProto.CombLeaderTempMsg.newBuilder();
		msg.setCombId(data.getCombId());
		msg.setLeaderId(data.getLeaderId());
		msg.setPromoteId(data.getPromoteId());
		msg.setLastChooseTime(data.getLastChooseTime());
		msg.setKeepSakePatronsId(data.getKeepSakePatronsId());
		msg.setKeepSakeTime(data.getKeepSakeTime());

		for (CombLeaderSkill skill : data.getSkillMap().values()) {
			msg.addSkill(parseCombLeaderSkillTempMsg(skill));
		}
		return msg.build();
	}

	public static CombLeaderProto.CombLeaderSkillTempMsg parseCombLeaderSkillTempMsg(CombLeaderSkill skill) {
		CombLeaderProto.CombLeaderSkillTempMsg.Builder builder = CombLeaderProto.CombLeaderSkillTempMsg.newBuilder();
		builder.setSkillId(skill.getId());
		builder.setSkillLv(skill.getLv());
		builder.setSkinLv(skill.getSkinLv());
		return builder.build();
	}
}
